package gui;

import java.awt.EventQueue;
import java.awt.TextArea;

import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.BorderLayout;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import java.awt.event.ActionListener;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Iterator;
import java.util.LinkedList;
import java.awt.event.ActionEvent;

public class guitext {

	private JFrame frame;
	private JTextField textField;
	private JTextArea textArea;
	private JButton btnNewButton_1, btnNewButton_2;
	private boolean B = false; // 判断中英互换
	static Connection con;
	static Statement sql;
	static ResultSet res;
	static PreparedStatement ps;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {

		EventQueue.invokeLater(new Runnable() {

			public void run() {
				try {
					guitext window = new guitext();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public guitext() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame("翻译(默认中译英)");
		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);

		JButton btnNewButton = new JButton("查询");// 按钮
		// 创建词典内容

		// 获得Connection 连接
		GetConnection g = new GetConnection();
		con = g.getConnection();

		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 默认中译英
				String edit;

				boolean bool = false; // 有无找到
				try {
					sql = con.createStatement();
				} catch (SQLException e2) {
					e2.printStackTrace();
				}
				// 英译中
				if (B && e.getSource() == btnNewButton) {
					// 获取编辑框内容
					String regex = "[a-zA-Z]+";
					edit = textField.getText();
					if (!edit.matches(regex)) {
						textArea.setText("输入正确 的英文字符");
						return;
					}
					edit = edit.toLowerCase();// 转换为小写
					// 匹配
					try {
						res = sql.executeQuery("select *from thesaurus where English='" + edit + "'");
						textArea.setText(null);

						while (res.next()) {
							bool = true;
							textArea.append(res.getString(1) + "\n");
							textArea.append(res.getString(3));
						}
					} catch (SQLException e1) {
						e1.printStackTrace();
					}

				}
				// 中译英
				if (!B && e.getSource() == btnNewButton) {
					// 获取编辑框内容
					String regex = "[\u4e00-\u9fa5]+";
					edit = textField.getText();
					if (!edit.matches(regex)) {
						textArea.setText("输入正确中文");
						return;
					}
					// 匹配
					// 中译英
					try {
						res = sql.executeQuery("select *from thesaurus where Chinese='" + edit + "'");

						textArea.setText(null);

						while (res.next()) {
							textArea.append(res.getString(2) + "\n");
							bool = true;
						}
					} catch (SQLException e1) {
						e1.printStackTrace();
					}
				}
				if (!bool) {
					textArea.setText("没有找到该词");

				}
			}

		});
		btnNewButton.setBounds(41, 102, 63, 27);
		frame.getContentPane().add(btnNewButton);

		textField = new JTextField();
		textField.setBounds(72, 66, 186, 27);
		frame.getContentPane().add(textField);
		textField.setColumns(10);

		textArea = new JTextArea();
		textArea.setBounds(57, 134, 210, 95);
		frame.getContentPane().add(textArea);

		btnNewButton_1 = new JButton("中译英");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				B = false;

			}
		});
		btnNewButton_1.setBounds(29, 11, 113, 27);
		frame.getContentPane().add(btnNewButton_1);

		btnNewButton_2 = new JButton("英译中");
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				B = true;
			}
		});

		btnNewButton_2.setBounds(206, 11, 113, 27);
		frame.getContentPane().add(btnNewButton_2);

		JButton btnNewButton_3 = new JButton("增加");
		btnNewButton_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				Add add = new Add(con);

			}
		});
		btnNewButton_3.setBounds(129, 102, 63, 27);
		frame.getContentPane().add(btnNewButton_3);

		JButton btnNewButton_4 = new JButton("删除");
		btnNewButton_4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				String str = textField.getText(); // 获取文本内容
				int i = 0;
				try {
					sql = con.createStatement();

					i = sql.executeUpdate("delete from thesaurus where Chinese='" + str + "' or English='" + str + "'");
					if (i == 1) {
						textArea.setText("删除成功");
						return;
					}
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
				textArea.setText("删除失败,不存在");
			}
		});
		btnNewButton_4.setBounds(206, 102, 63, 27);
		frame.getContentPane().add(btnNewButton_4);

		JButton btnNewButton_5 = new JButton("修改");
		btnNewButton_5.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 修改
				Modify modify = new Modify(con);

			}
		});
		btnNewButton_5.setBounds(291, 102, 63, 27);
		frame.getContentPane().add(btnNewButton_5);
	}
}
